package com.cgy.utils.spider.element;

import java.util.List;
import java.util.Map;

import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import com.cgy.utils.spider.latch.AbstractLatch;
import com.cgy.utils.spider.pojo.Element;
import com.cgy.utils.spider.type.ElementType;
import com.google.common.collect.Lists;

import lombok.Getter;
import lombok.Setter;

/**
 * 基础节点配置
 * @author CH
 *
 */
@Getter
@Setter
public abstract class AbstractElement {

	private List<Element> lElements;
	private ElementType element;
	private boolean isDebugger = false;
	private AbstractLatch latch;
	/**
	 * 获取基本数据
	 */
	private Map<String, Object> result;
	@Accessors(fluent = true)
	private boolean download = false;


	public AbstractElement() {
		beforeElement();
		this.element = ElementType.CUSTOM;
		this.lElements = Lists.newArrayList();
	}
	
	public AbstractElement(ElementType elementType) {
		beforeElement();
		this.element = elementType;
		this.lElements = Lists.newArrayList();
	}
	/**
	 * 结果处理
	 * @return
	 */
	public AbstractElement result(AbstractLatch latch) {
		this.latch = latch;
		return this;
	}

	/**
	 * 构造节点
	 * @return
	 */
	public AbstractElement build() {
		element();
		return this;
	}
	/**
	 * 添加配置
	 * @return
	 */
	public AbstractElement addElement(String name, String xpath, ElementType elementType) {
		if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(xpath)) {
			Element element2 = new Element();
			element2.setName(name);
			element2.setXpath(xpath);
			element2.setElementType(null == elementType ? this.element : elementType);
			this.lElements.add(element2);
		}
		return this;
	}


	/**
	 *
	 * @return
	 */
	abstract public AbstractElement element();

	/**
	 *
	 * @return
	 */
	protected AbstractElement beforeElement() {
		return this;
	}


}
